package biz.datalk.industrialland.admin.modules.system.controller;

import biz.datalk.industrialland.admin.modules.system.pojo.dto.SysFunctionDTO;
import biz.datalk.industrialland.admin.modules.system.service.ISysFunctionService;
import biz.datalk.industrialland.common.def.CommonConstant;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.log.aspect.annotation.SysLog;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.validated.BatchDelete;
import biz.datalk.industrialland.common.validated.Delete;
import biz.datalk.industrialland.common.validated.Insert;
import biz.datalk.industrialland.common.validated.Update;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author tarofang@163.com
 * @date 2019-09-09
 */
@RestController
@RequestMapping("/sys/function")
public class SysFunctionController {

    private ISysFunctionService sysFunctionService;

    @Autowired
    public SysFunctionController(ISysFunctionService sysFunctionService) {
        this.sysFunctionService = sysFunctionService;
    }


    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysFunctionDTO.class, filter = "createTime,updateTime")
    @SysLog(OptEnum.LAND_FUNC_10801)
    public UnifyResult list(@RequestBody SysFunctionDTO sysFunctionDTO) {
        return sysFunctionService.list(sysFunctionDTO);
    }

    @RSAES
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_FUNC_10802)
    public UnifyResult add(@RequestBody @Validated({Insert.class}) SysFunctionDTO sysFunctionDTO) {
        return sysFunctionService.add(sysFunctionDTO);
    }

    @RSAES
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_FUNC_10803)
    public UnifyResult update(@RequestBody @Validated({Update.class}) SysFunctionDTO sysFunctionDTO) {
        return sysFunctionService.update(sysFunctionDTO);
    }

    @RSAES
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_FUNC_10804)
    public UnifyResult delete(@RequestBody @Validated({Delete.class}) SysFunctionDTO sysFunctionDTO) {
        return sysFunctionService.delete(sysFunctionDTO);
    }

    @RSAES
    @PostMapping(value = "/batchDelete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_FUNC_10805)
    public UnifyResult batchDelete(@RequestBody @Validated({BatchDelete.class}) SysFunctionDTO sysFunctionDTO) {
        return sysFunctionService.batchDelete(sysFunctionDTO);
    }


    @RSAES
    @RequestMapping(value = "/getUserPermissionByToken", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_FUNC_10806)
    public UnifyResult getUserPermissionByToken(HttpServletRequest request) {
        String token = StringUtils.trimToEmpty(request.getHeader(CommonConstant.ACCESS_TOKEN));
        token = StringUtils.isNotBlank(token) ? token : StringUtils.trimToEmpty(request.getParameter("token"));

        return sysFunctionService.getUserPermissionByToken(token);
    }


}
